/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jpChungLoai.java
 *
 * Created on Dec 15, 2011, 9:53:19 PM
 */
package GUI;
import java.sql.SQLException;
import java.util.Vector;
import Bussiness.ChungLoai;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Nlanh_BN
 */
public class FrmChungLoai extends javax.swing.JFrame {
    static String bot;
    private DefaultTableModel modelTable = new DefaultTableModel();
    /** Creates new form jpChungLoai */
    public FrmChungLoai() {
        initComponents();
        this.display();
    }
     public void reset()
    {
        txtMachungloai.setText("");
        txtTenchungloai.setText("");
        
    }
      public void add()
    {
        ChungLoai b = new  ChungLoai();
        b.setMachungloai(txtMachungloai.getText());
        b.setChungloai(txtTenchungloai.getText());
       
        int t = 0;
        try{
            t = b.InsertChungloai(b);
        }catch(SQLException e){
            e.printStackTrace();
        }
        if(t==1)
            JOptionPane.showMessageDialog(this,"Add successful","Infofrm",JOptionPane.INFORMATION_MESSAGE);
        else
            JOptionPane.showMessageDialog(this,"Add erro!");
       this.reset();
    }
       public void update()
    {
         ChungLoai c = new ChungLoai();
        c.setMachungloai(txtMachungloai.getText());
        c.setChungloai(txtTenchungloai.getText());
        int t = 0;
        try{
            t = c.UpdateChungLoai(c);
        }catch(SQLException e){
            e.printStackTrace();
        }
        if(t==1)
            JOptionPane.showMessageDialog(this,"UPdate successful","Infofrm",JOptionPane.INFORMATION_MESSAGE);
        else
            JOptionPane.showMessageDialog(this,"Update erro!");
       
    }
public void loadTable(Vector v)
    {
        modelTable.setNumRows(0);
        Vector v1 = new Vector();
        v1.addElement("Mã chủng loại");
        v1.addElement("Chủng loại");
        modelTable.setColumnIdentifiers(v1);
        ChungLoai b = new ChungLoai();
        for (int i = 0; i < v.size(); i++) {
            b = (ChungLoai)v.get(i);
            Vector row = new Vector();
            row.addElement(b.getMachungloai());
            row.addElement(b.getChungloai());
            modelTable.addRow(row);
        }
        tblChungLoai.setModel(modelTable);
        int r = tblChungLoai.getSelectedRow();
        if(r < 0){
            this.bot = "";
//            jButtonSua.setEnabled(false);
//            jButtonXoa.setEnabled(false);
            return;
        }
        else{
            bot = tblChungLoai.getValueAt(r, 0).toString();
        }
    }  
 public void display()
    {
        ChungLoai b1 = new ChungLoai();
        Vector v1 = new Vector();
        try {
           v1=b1.getAllChungLoai(); 
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.loadTable(v1);
    }
 
//**** tìm kiếm >>?? không biết là phải tìm kiếm cái gì đây??
//public void search(){
//        ChungLoai b=new ChungLoai();
//        Vector v=new Vector();        
//        try {
//            if (cbSearch.getSelectedIndex()==0)
//                v=b.getNCC_MaNCC(txtSearch.getText());
//            else
//                v=b.getNCC_TenNCC(txtSearch.getText());
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }  
//          this.loadTable(v);
//    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtTenchungloai = new javax.swing.JTextField();
        txtMachungloai = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnLoad = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblChungLoai = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Mã Chủng Loại");

        jLabel2.setText("Tên Chủng Loại");

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnLoad.setText("Load");
        btnLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadActionPerformed(evt);
            }
        });

        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        tblChungLoai.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblChungLoai.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblChungLoaiMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblChungLoai);

        jLabel3.setText("QUẢN LÝ CHỦNG LOẠI");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(btnAdd)
                                .addComponent(jLabel2)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnEdit)
                                .addGap(46, 46, 46)
                                .addComponent(btnDelete)
                                .addGap(26, 26, 26)
                                .addComponent(btnLoad)
                                .addGap(29, 29, 29)
                                .addComponent(btnReset))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtTenchungloai, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtMachungloai, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(192, 192, 192)
                        .addComponent(jLabel3)))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtMachungloai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtTenchungloai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEdit)
                    .addComponent(btnDelete)
                    .addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnLoad))
                .addGap(40, 40, 40)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(211, 211, 211))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
// TODO add your handling code here:
    this.add();
    this.display();
    
}//GEN-LAST:event_btnAddActionPerformed

private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
// TODO add your handling code here:
    this.update();
    this.display();
}//GEN-LAST:event_btnEditActionPerformed

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
// TODO add your handling code here:
     if(!bot.equals("")){
        int show=JOptionPane.showConfirmDialog(this,"Are You sure delete ?","Inform",JOptionPane.YES_NO_OPTION);
        int t=0,count=0;
        ChungLoai b=new ChungLoai();
        if(show==JOptionPane.YES_OPTION){
            int [] rArr=tblChungLoai.getSelectedRows();
                try 
                {
                    for(int i=0;i<rArr.length;i++)
                    {
                        t=b.DelChungLoai(tblChungLoai.getValueAt(rArr[i],0).toString());
                        if(t==1)
                            count++;
                    }
                    if(count>0)
                    {
                        JOptionPane.showMessageDialog(this,"Deleted "+count+" record ","successful.",JOptionPane.INFORMATION_MESSAGE);
                        this.display();
                    }
                    else
                    {
                      JOptionPane.showMessageDialog(this,"Error","Inform",JOptionPane.ERROR_MESSAGE);  
                    }
                } catch (SQLException x) {               
                   System.out.println(x);
                }
        }
    }
    this.reset();
}//GEN-LAST:event_btnDeleteActionPerformed

private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadActionPerformed
// TODO add your handling code here:
    this.display();
}//GEN-LAST:event_btnLoadActionPerformed

private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
// TODO add your handling code here:
     this.reset();
}//GEN-LAST:event_btnResetActionPerformed

private void tblChungLoaiMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblChungLoaiMouseClicked
// TODO add your handling code here:
     int r = tblChungLoai.getSelectedRow();
    if(r < 0){
        bot = "";
        return;
    }else{
        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);
        bot = tblChungLoai.getValueAt(r, 0).toString();
        
        if (r != -1){
            txtMachungloai.setText(String.valueOf(tblChungLoai.getValueAt(r, 0)));
            txtTenchungloai.setText(String.valueOf(tblChungLoai.getValueAt(r, 1)));
            
        }
    }
}//GEN-LAST:event_tblChungLoaiMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmChungLoai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmChungLoai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmChungLoai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmChungLoai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FrmChungLoai().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnLoad;
    private javax.swing.JButton btnReset;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable tblChungLoai;
    private javax.swing.JTextField txtMachungloai;
    private javax.swing.JTextField txtTenchungloai;
    // End of variables declaration//GEN-END:variables
}
